package com.t2.t2app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler  {

    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Config.WEI_XIN_APPID, false);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        finish();
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
            SendAuth.Resp authResp = (SendAuth.Resp) resp;
            switch (authResp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    // 授权成功，将 code 发送给 Flutter
                    String code = authResp.code;
                    // 这里可以通过 EventChannel 等方式将 code 传递给 Flutter
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    // 用户取消授权
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:
                    // 授权被拒绝
                    break;
                default:
                    // 其他错误
                    break;
            }
        }
        finish();
    }
}